/**
 * Created by liebling on 2017/7/9.
 */
const keystone = require('keystone');
const Promise = require('bluebird');
const _ = require('lodash');
const Post = keystone.list('User');

exports = module.exports = function (req, res) {

	var view = new keystone.View(req, res);
	var locals = res.locals;

	// locals.section is used to set the currently selected
	// item in the header navigation.
	locals.section = 'userDetail';
	locals.pageName = 'userCenter/userDetail';
	
	let reqQuery = _.omit(_.assign(req.query, req.body, req.params), []);
	let omitFueld = ['password','isAdmin','__v'];
	let optionalExpression = _.transform(omitFueld,function (r,val) {
		r[val] = 0;
},{});
	if (_.has(reqQuery, 'id') && reqQuery.id != '') {
		Post.model.findById(reqQuery.id,optionalExpression).populate('userGroup')
			.exec(function (err, result) {
				if (err) return res.json({code: 0, message: '请求失败'});
				if (_.isEmpty(result)) return res.json({code: 0, message: '找不到用户'});
				locals.user = result;
				return view.render('userCenter/userDetail');
			});
	} else {
		return res.json({code: 0, message: '用户ID为空'});
	}
	
};
